package com.niiwoo.civet.mobile.controller;

import com.alibaba.dubbo.common.utils.Assert;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.niiwoo.civet.account.dto.request.BillQueryRequest;
import com.niiwoo.civet.account.dto.request.HandAddBillAndChangeBalanceDTO;
import com.niiwoo.civet.account.dto.request.MonthBillRequestDTO;
import com.niiwoo.civet.account.dto.response.*;
import com.niiwoo.civet.account.enums.*;
import com.niiwoo.civet.account.enums.BillFirstSubjectEnum.IndexBillTypeEnum;
import com.niiwoo.civet.account.service.NiiwooBillDubboService;
import com.niiwoo.civet.base.enums.ChannelCodeEnum;
import com.niiwoo.civet.mobile.service.bill.InvokeTradeService;
import com.niiwoo.civet.mobile.vo.request.BillTypesByInoutTypeRequestVO;
import com.niiwoo.civet.mobile.vo.request.IndexTypeRequestVO;
import com.niiwoo.civet.mobile.vo.request.bill.*;
import com.niiwoo.civet.mobile.vo.response.niiwoobill.*;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.ProjectPackageInvestOrderDTO;
import com.niiwoo.civet.trade.service.intelligenceInvest.IntelligenceOrderDubboService;
import com.niiwoo.civet.user.service.UserBasicInfoDubboService;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;
import com.niiwoo.tripod.web.annotation.AuthIgnore;
import com.niiwoo.tripod.web.shiro.UserAuthPrincipal;
import com.niiwoo.tripod.web.util.PrincipalContext;
import com.niiwoo.tripod.web.vo.PageResponseVO;
import com.niiwoo.tripod.web.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by Jaylin on 2017/11/14.
 * 用户账单相关信息controller
 */
@RestController
@RequestMapping("/user/bills")
@Api(tags = "用户账单(殷俊玲,肖应平)", description = "/user/bills")
@Slf4j
public class NiiwooUserBillController {

    @Reference(version = "1.0.0")
    private NiiwooBillDubboService niiwooBillDubboService;
    @Reference(version = "1.0.0")
    private UserBasicInfoDubboService userBasicInfoDubboService;
    @Reference(version = "1.0.0")
    private IntelligenceOrderDubboService intelligenceOrderDubboService;

    @Autowired
    private InvokeTradeService invokeTradeService;

    @Value("${h5-invest-detail-url}")
    private String h5InvestDetailUrl;
    @Value("${h5-borrow-detail-url}")
    private String h5BorrowDetailUrl;

    @PostMapping("/type/ios")
    @ApiOperation("收支检索类型-暂时不用这个接口")
    public Result<List<BillCommonTypeVO>> inOutType() {
        log.info("not use interface inOutType");
        List<BillCommonTypeVO> list = new ArrayList<>();
        for (BillSecondSubjectEnum.InoutTypeEnum typeEnum : BillSecondSubjectEnum.InoutTypeEnum.values()) {
            list.add(new BillCommonTypeVO(String.valueOf(typeEnum.getType()), typeEnum.getDesc()));
        }

        return Result.with(list);
    }

    @PostMapping("/type/subjects")
    @ApiOperation("科目名称检索类型-暂时不用这个接口")
    public Result<List> indexBillType() {
        log.info("not use interface indexBillType");
        List<BillCommonTypeVO> list = new ArrayList<>();
        for (IndexBillTypeEnum typeEnum : IndexBillTypeEnum.values()) {
            list.add(new BillCommonTypeVO(typeEnum.getIndexCode(), typeEnum.getIndexDesc()));
        }

        return Result.with(list);
    }

    @PostMapping("/type/billType")
    @ApiOperation("账单检索类型:请求入口类型-参数范围1001-账单,1002-担保账单,1003-投资账单,1004-通知入口进入账单 1005-借款账单 1006-尽调账单")
    public Result<List<BillCommonTypeVO>> listBillTypes(@RequestBody IndexTypeRequestVO requestVO) {
        log.info("not use interface listBillTypes");
        List<BillCommonTypeVO> list = new ArrayList<>();

        if (StringUtils.isEmpty(requestVO.getRequestType()) || EntranceTypeEnum.ALL_BILL.getCode().equals(requestVO.getRequestType())) {
            list = allBill();
        } else if (EntranceTypeEnum.INVEST_BILL.getCode().equals(requestVO.getRequestType())) {
            list = investBill();
        } else if (EntranceTypeEnum.GUARANTEE_BILL.getCode().equals(requestVO.getRequestType())) {
            list = guaranteeBill();
        } else if (EntranceTypeEnum.INFORM_ENTRANCE_BILL.getCode().equals(requestVO.getRequestType())) {
            list = informBill();
        } else {

        }

        return Result.with(list);
    }

    @PostMapping("/billTypesByInoutType")
    @ApiOperation("获取账单类型集合：通过收支类型+场景类型")
    @AuthIgnore
    public Result<List<BillCommonTypeVO>> billTypesByInoutType(@RequestBody BillTypesByInoutTypeRequestVO billTypesByInoutTypeRequestVO) {
        log.info("not use interface billTypesByInoutType");
        List<BillCommonTypeVO> list = new ArrayList<>();

        Byte inoutType = billTypesByInoutTypeRequestVO.getInoutType();
        if(Objects.nonNull(inoutType)){
            if(StringUtils.equals("10",billTypesByInoutTypeRequestVO.getSceneType())){
                if(InoutTypeEnum.IN_TYPE.getType().equals(inoutType)){
                    list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_RECHARGE.getIndexCode(), IndexBillTypeEnum.INDEX_RECHARGE.getIndexDesc()));
                    list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_WITHDRAW.getIndexCode(), IndexBillTypeEnum.INDEX_WITHDRAW.getIndexDesc()));
                    list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_INVEST_BONUS.getIndexCode(), IndexBillTypeEnum.INDEX_INVEST_BONUS.getIndexDesc()));
                    list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_INVEST_BACK.getIndexCode(), IndexBillTypeEnum.INDEX_INVEST_BACK.getIndexDesc()));
                }else if(InoutTypeEnum.OUT_TYPE.getType().equals(inoutType)) {
                    list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_WITHDRAW.getIndexCode(), IndexBillTypeEnum.INDEX_WITHDRAW.getIndexDesc()));
                    list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_INVEST.getIndexCode(), IndexBillTypeEnum.INDEX_INVEST.getIndexDesc()));
                }
            }
        }

        return Result.with(list);
    }

    @PostMapping
    @ApiOperation("查询用户账单列表")
    public Result<BillsPageResponseVO> listUserBills(@RequestBody UserBillListRequestVO requestVO) {
        log.info("not use interface listUserBills");
        Long startTime = System.currentTimeMillis();
        log.info("listUserBills start, request:{}", JSON.toJSONString(requestVO));
        String userId = PrincipalContext.getUserId();

        BillQueryRequest billQueryRequest = new BillQueryRequest();
        billQueryRequest.setUserId(userId);
        billQueryRequest.setIsVisibleEnum(BillIsVisibleEnum.SHOW);
        billQueryRequest.setPageNumber(requestVO.getPageNumber());
        billQueryRequest.setPageSize(requestVO.getPageSize());
        billQueryRequest.setInoutType(BillSecondSubjectEnum.InoutTypeEnum.getEnumByCode(requestVO.getInoutType()));
        billQueryRequest.setMinBillTime(requestVO.getMinBillTime());
        billQueryRequest.setMaxBillTime(requestVO.getMaxBillTime());

        //对查询条件进行处理
        billTypeParams(requestVO,billQueryRequest);

        //调用账单接口进行查询
        BillsPageResponseDTO billsResponseDTO = niiwooBillDubboService.pageUserBills(billQueryRequest);
        BillsPageResponseVO billsResponseVO = new BillsPageResponseVO();
        if (Objects.nonNull(billsResponseDTO.getItems())) {
            //对结果集进行处理
            List<UserBillsResponseVO> responseVOList = listBillsResponseDTO(billsResponseDTO.getItems(), requestVO.getRequestType(), requestVO.getBillType());

            billsResponseVO.setItems(responseVOList);
            billsResponseVO.setTotalPage(billsResponseDTO.getTotalPage());
            billsResponseVO.setTotalCount(billsResponseDTO.getTotalCount());
            billsResponseVO.setTotalPage(billsResponseDTO.getTotalPage());
        }

        if (StringUtils.isNotEmpty(billsResponseDTO.getTotalDesc())) {
            billsResponseVO.setTotalDesc(billsResponseDTO.getTotalDesc());
        }

        Long endTime = System.currentTimeMillis();
        log.info("listUserBills end, cost:{} ms, request:{}", endTime - startTime, requestVO);
        return Result.with(billsResponseVO);
    }

    @PostMapping("/forPC")
    @ApiOperation("PC端分页查询用户账单列表")
    public Result<PageResponseVO<UserBillForPCResponseVO>> pageUserBillsForPC(@RequestBody UserBillListRequestVO requestVO) {
        Long startTime = System.currentTimeMillis();
        log.info("pageUserBillsForPC start, request:{}", JSON.toJSONString(requestVO));
        String userId = PrincipalContext.getUserId();

        BillQueryRequest billQueryRequest = new BillQueryRequest();
        billQueryRequest.setUserId(userId);
        billQueryRequest.setIsVisibleEnum(BillIsVisibleEnum.SHOW);
        billQueryRequest.setPageNumber(requestVO.getPageNumber());
        billQueryRequest.setPageSize(requestVO.getPageSize());
        if(Objects.nonNull(requestVO.getInoutType())){
            billQueryRequest.setInoutType(BillSecondSubjectEnum.InoutTypeEnum.getEnumByCode(requestVO.getInoutType()));
        }
        billQueryRequest.setMinBillTime(requestVO.getMinBillTime());
        billQueryRequest.setMaxBillTime(requestVO.getMaxBillTime());

        //对查询条件进行处理
        billQueryBillType(requestVO.getBillType(), billQueryRequest);

        //调用账单接口进行查询
        PageResponseDTO<NiiwooBillRespDTO> billsResponseDTO = niiwooBillDubboService.queryUserBills(billQueryRequest);
        List<UserBillForPCResponseVO> billResponseVOList = new ArrayList<>();
        for (NiiwooBillRespDTO dto : billsResponseDTO.getItems()) {
            UserBillForPCResponseVO responseVO = new UserBillForPCResponseVO();
            responseVO.setBillId(dto.getBillId());
            responseVO.setFirstSubjectName(BillFirstSubjectEnum.getEnumByCode(dto.getFirstSortCode()).getCodeName());
            responseVO.setSecondSubjectName(BillSecondSubjectEnum.getEnumByCode(dto.getSecondSortCode()).getCodeName());
            responseVO.setAmount(dto.getAmount());
            responseVO.setInOutType(dto.getInoutType());
            responseVO.setStatus(dto.getStatus());
            responseVO.setCreateTime(dto.getCreateTime());
            billResponseVOList.add(responseVO);
        }

        PageResponseVO<UserBillForPCResponseVO> pageResponseVO = new PageResponseVO<>();
        pageResponseVO.setItems(billResponseVOList);
        pageResponseVO.setTotalPage(billsResponseDTO.getTotalPage());
        pageResponseVO.setTotalCount(billsResponseDTO.getTotalCount());
        pageResponseVO.setTotalPage(billsResponseDTO.getTotalPage());

        Long endTime = System.currentTimeMillis();
        log.info("pageUserBillsForPC end, cost:{} ms, request:{}", endTime - startTime, requestVO);
        return Result.with(pageResponseVO);
    }


    @PostMapping("/detail")
    @ApiOperation("账单详情")
    public Result<NiiwooBillDetailReponseVO> queryBillDetail(@RequestBody @Valid BillDetailRequestVO requestVO) {
        Long startTime = System.currentTimeMillis();
        log.info("listUserBills start, request:{}", requestVO);

        String userId = PrincipalContext.getUserId();
        if (Objects.isNull(requestVO.getBillId())) Assert.notNull(requestVO.getBillId(), "账单不能为空");

        NiiwooBillDetailReponseDTO responseDTO = niiwooBillDubboService.getUserBillDetail(userId, requestVO.getBillId());

        List<BillDetailReponseVO> billDetailVOS = new ArrayList<>();
        NiiwooBillDetailReponseVO reponseVO = new NiiwooBillDetailReponseVO();
        reponseVO.setOrderId(responseDTO.getOrderId());
        reponseVO.setAmount(responseDTO.getAmount());
        reponseVO.setInOutType(responseDTO.getInoutType());
        reponseVO.setJumpType(responseDTO.getJumpType());
        reponseVO.setJumpTypeId(responseDTO.getJumpTypeId());
        reponseVO.setTitle(responseDTO.getTitle());
        reponseVO.setTitleDown(responseDTO.getTitleDown());
        reponseVO.setNeedJump(responseDTO.getNeedJump());
        reponseVO.setBillDetailVOs(billDetailVOS);

        List<BillDetailReponseDTO> reponseDTOS = responseDTO.getDetailDTOs();
        for (BillDetailReponseDTO dto : reponseDTOS) {
            billDetailVOS.add(new BillDetailReponseVO(dto.getDetailKey(), dto.getDetailValue()));
        }
        if (Objects.nonNull(responseDTO.getJumpType())) {
            if(responseDTO.getJumpType() == 1) {
                //1-借款
                reponseVO.setJumpUrl(this.h5BorrowDetailUrl + responseDTO.getJumpTypeId());
            } else if(responseDTO.getJumpType() == 2) {
                // 2-投资
                reponseVO.setJumpUrl(this.h5InvestDetailUrl + responseDTO.getJumpTypeId());
                if(!responseDTO.getProjectPackageId().equals(responseDTO.getProjectId())){
                    //智能投
                    reponseVO.setInvestOrderType(2);
                    ProjectPackageInvestOrderDTO investOrderDTO = intelligenceOrderDubboService.getProjectPackageInvestOrderBySubInvestOrderId(Long.valueOf(responseDTO.getJumpTypeId()));
                    reponseVO.setJumpTypeId(investOrderDTO.getId().toString());
                }else {
                    //普通投
                    reponseVO.setInvestOrderType(1);
                    reponseVO.setJumpTypeId(responseDTO.getJumpTypeId());
                }
            }

        }

        Long endTime = System.currentTimeMillis();
        log.info("listUserBills end, cost:{} ms, request:{}", endTime - startTime, requestVO);
        return Result.with(reponseVO);
    }

    @PostMapping("/projects")
    @ApiOperation("用户账单按照项目分组")
    public Result<PageResponseVO<NiiwooUserProjectBillResponseVO>> listBillsGroupByProject(@RequestBody PageProjectRequestVO pageRequestVO) {
        String userId = PrincipalContext.getUserId();

        BillQueryRequest billQueryRequest = new BillQueryRequest();
        billQueryRequest.setUserId(userId);
        billQueryRequest.setPageNumber(pageRequestVO.getPageNumber());
        billQueryRequest.setPageSize(pageRequestVO.getPageSize());
        //requestType(pageRequestVO.getRequestType(), billQueryRequest);

        //分页查询
        PageResponseDTO<NiiwooUserProjectBillReponseDTO> pageResponseDTO = niiwooBillDubboService.pageUserBillsGroupByPackage(billQueryRequest);

        //组装返回结果
        List<NiiwooUserProjectBillReponseDTO> responseDTOList = pageResponseDTO.getItems();
        List<NiiwooUserProjectBillResponseVO> responseVOList = new ArrayList<>();
        for (NiiwooUserProjectBillReponseDTO responseDTO : responseDTOList) {
            NiiwooUserProjectBillResponseVO responseVO = new NiiwooUserProjectBillResponseVO();
            responseVO.setBillId(responseDTO.getBillId());
            responseVO.setCreateTime(responseDTO.getCreateTime());
            responseVO.setOrderId(responseDTO.getOrderId());
            responseVO.setTitle(responseDTO.getTitle());
            responseVO.setProjectId(responseDTO.getProjectPackageId());
            responseVO.setRelationImgSrc(responseDTO.getBillImgSrc());
            responseVO.setAccountType(responseDTO.getAccountType());
            responseVO.setAmountTotalDetails(responseDTO.getAmountTotalDetails());

            responseVOList.add(responseVO);
        }

        PageResponseVO<NiiwooUserProjectBillResponseVO> pageResponseVO = new PageResponseVO<>();
        pageResponseVO.setItems(responseVOList);
        pageResponseVO.setTotalPage(pageResponseDTO.getTotalPage());
        pageResponseVO.setTotalCount(pageResponseDTO.getTotalCount());

        return Result.with(pageResponseVO);
    }

    @PostMapping("/projectDetail")
    @ApiOperation("项目账单详情列表")
    public Result<List<NiiwooProjectBillDetailResponseVO>> queryProjectBillDetail(@RequestBody @Valid ProjectListRequestVO requestVO) {
        String userId = PrincipalContext.getUserId();
        if (Strings.isNullOrEmpty(userId)) return null;

        BillQueryRequest billQueryRequest = new BillQueryRequest();
        billQueryRequest.setUserId(userId);
        billQueryRequest.setProjectPackageId(requestVO.getProjectId());
        List<NiiwooUserBillResponseDTO> userBills = niiwooBillDubboService.listUserBills(billQueryRequest);

        List<NiiwooProjectBillDetailResponseVO> responseVOList = new ArrayList<>();
        for (NiiwooUserBillResponseDTO userBill : userBills) {
            NiiwooProjectBillDetailResponseVO responseVO = new NiiwooProjectBillDetailResponseVO();
            responseVO.setBillId(userBill.getBillId());
            responseVO.setOrderId(userBill.getOrderId());
            responseVO.setAmount(userBill.getAmount());
            responseVO.setStatus(userBill.getStatus());
            responseVO.setStatusDesc(BillStatusEnum.getEnumByCode(userBill.getStatus()).getDesc());
            responseVO.setTitle(userBill.getTitle());
            responseVO.setCreateTime(userBill.getCreateTime());

            responseVOList.add(responseVO);
        }


        return Result.with(responseVOList);
    }

    @PostMapping("/income/months")
    @ApiOperation("月份收入列表-暂时不用")
    public Result<List> listBillMonthIncome() {
        log.info("not use interface listBillMonthIncome");
        UserAuthPrincipal principal = (UserAuthPrincipal) SecurityUtils.getSubject().getPrincipal();
        String userId = principal.getUserId();
        if (Strings.isNullOrEmpty(userId)) return null;

        List<MonthBillCountResponseDTO> countDTOList = niiwooBillDubboService.listMonthBill(userId);
        List<NiiwooBillMonthResponseVO> responseVOList = new ArrayList<>();
        for (MonthBillCountResponseDTO countDTO : countDTOList) {
            NiiwooBillMonthResponseVO responseVO = new NiiwooBillMonthResponseVO();
            responseVO.setYearMonth(countDTO.getYearMonth());
            responseVO.setAmount(countDTO.getMonthIncome());

            responseVOList.add(responseVO);
        }
        return Result.with(responseVOList);
    }

    @PostMapping("/monthBillDetail")
    @ApiOperation("按照月份查询账单科目相关统计-暂时不用")
    public Result<MonthBillResponseVO> queryMonthBillDetailByMonth(@RequestBody NiiwooMonthRequestVO requestVO) {
        log.info("not use interface queryMonthBillDetailByMonth");
        String userId = PrincipalContext.getUserId();

        MonthBillRequestDTO requestDTO = new MonthBillRequestDTO();
        requestDTO.setYearMonth(requestVO.getMonth());
        requestDTO.setUserId(userId);

        MonthBillResponseVO responseVO = new MonthBillResponseVO();
        MonthBillResponseDTO responseDTO = niiwooBillDubboService.queryMonthBillDetailByMonth(requestDTO);
       /* responseVO.setBorrowAmount(responseDTO.getBorrowAmount());
        responseVO.setBorrowServiceFee(responseDTO.getBorrowServiceFee());
        responseVO.setGuaranteeFee(responseDTO.getGuaranteeFee());
        responseVO.setGuaranteeIncome(responseDTO.getGuaranteeIncome());
        responseVO.setInterestIncome(responseDTO.getInterestIncome());
        responseVO.setInvestAmount(responseDTO.getInvestAmount());
        responseVO.setInvestReceive(responseDTO.getInvestReceive());
        responseVO.setInvestServiceFee(responseDTO.getInvestServiceFee());
        responseVO.setPlatformReward(responseDTO.getPlatformReward());
        responseVO.setPlatformSend(responseDTO.getPlatformSend());
        responseVO.setRechargeAmount(responseDTO.getRechargeAmount());
        responseVO.setResearch(responseDTO.getResearch());
        responseVO.setWithdrawServiceFee(responseDTO.getWithdrawServiceFee());
        responseVO.setRewardAmount(responseDTO.getRewardAmount());*/

        return Result.with(responseVO);
    }



    @PostMapping("/handAddBillAndChangeBalance_YwruioUIIbdeou21348fhd")
    @ApiOperation("手工增加账单-临时调用")
    @AuthIgnore
    public void handAddBillAndChangeBalance(@RequestBody HandAddBillAndChangeBalanceRequestDTO requestDTO) {
        log.info("handAddBillAndChangeBalance start:{}", JSON.toJSONString(requestDTO));

        HandAddBillAndChangeBalanceDTO handAddBillAndChangeBalanceDTO = new HandAddBillAndChangeBalanceDTO();
        handAddBillAndChangeBalanceDTO.setAccountId(requestDTO.getAccountId());
        handAddBillAndChangeBalanceDTO.setAmount(requestDTO.getAmount());
        handAddBillAndChangeBalanceDTO.setInoutType(InoutTypeEnum.getEnumByCode(requestDTO.getInoutType()));
        handAddBillAndChangeBalanceDTO.setStatus(BillStatusEnum.getEnumByCode(requestDTO.getStatus()));
        handAddBillAndChangeBalanceDTO.setChannelCode(ChannelCodeEnum.enumOf(requestDTO.getChannelCode()));
        handAddBillAndChangeBalanceDTO.setRemark(requestDTO.getRemark());
        handAddBillAndChangeBalanceDTO.setRelationAccountId(requestDTO.getRelationAccountId());
        handAddBillAndChangeBalanceDTO.setBillSecondSubjectEnum(BillSecondSubjectEnum.getEnumByCode(requestDTO.getBillSecondSubjectCode()));
        handAddBillAndChangeBalanceDTO.setIsVisible(requestDTO.getIsVisible());
        niiwooBillDubboService.handAddBillAndChangeBalance(handAddBillAndChangeBalanceDTO);

        log.info("handAddBillAndChangeBalance end:{}");
    }


    private List<UserBillsResponseVO> listBillsResponseDTO(List<UserBillsResponseDTO> userBillsResponseDTOs, String requestType, String billType) {
        List<UserBillsResponseVO> responseVOList = new ArrayList<>();

        for (UserBillsResponseDTO responseDTO : userBillsResponseDTOs) {
            UserBillsResponseVO userBillsResponseVO = new UserBillsResponseVO();
            List<NiiwooUserBillResponseDTO> reponseDTOList = responseDTO.getReponseDTOList();
            List<NiiwooBillResponseVO> userBillVOs = new ArrayList<>();

            for (NiiwooUserBillResponseDTO dto : reponseDTOList) {
                NiiwooBillResponseVO responseVO = new NiiwooBillResponseVO();
                responseVO.setBillId(dto.getBillId());
                responseVO.setOrderId(dto.getOrderId());
                responseVO.setTitle(dto.getTitle());
                responseVO.setAmount(dto.getAmount());
                responseVO.setBankCode(dto.getBankCode());
                responseVO.setStatus(dto.getStatus());
                responseVO.setCreateTime(dto.getCreateTime());
//                responseVO.setYearMonth(dto.getYearMonth());
                responseVO.setInOutType(dto.getInOutType());
                responseVO.setRelationImgSrc(dto.getRelationImgSrc());
                responseVO.setAccountType(dto.getAccountType());

                userBillVOs.add(responseVO);
            }

            userBillsResponseVO.setYear(responseDTO.getYearMonth().substring(0, 4));
            userBillsResponseVO.setMonth(responseDTO.getYearMonth().substring(4, 6));
            boolean flag = displayTotalMes(requestType, billType);
            if (flag) {
                userBillsResponseVO.setYearMonthTotalAmount(responseDTO.getYearMonthTotalAmount());
            }
            userBillsResponseVO.setReponseDTOList(userBillVOs);

            responseVOList.add(userBillsResponseVO);
        }

        return responseVOList;
    }

    /**
     * 参数赋值
     * @param requestVO
     * @param billQueryRequest
     */
    private void billTypeParams(UserBillListRequestVO requestVO,BillQueryRequest billQueryRequest) {
        //账单上面的筛选条件
        billQueryBillType(requestVO.getBillType(), billQueryRequest);

        //查询账单的入口
        requestType(requestVO.getRequestType(), billQueryRequest);

        //查看订单下的账单
        if(StringUtils.isNotEmpty(requestVO.getOrderId())) {
            billQueryRequest.setOrderId(requestVO.getOrderId());
        }

        //查看投资订单下的账单
        if(StringUtils.isNotEmpty(requestVO.getInvestId())) {
            billQueryRequest.setInvestId(Long.parseLong(requestVO.getInvestId()));
        }
    }

    private void billQueryBillType(String billType, BillQueryRequest billQueryRequest) {
        if (StringUtils.isNotEmpty(billType)) {
            billQueryRequest.setIndexBillType(BillFirstSubjectEnum.IndexBillTypeEnum.getEnumByCode(billType));
            if (String.valueOf(BillSecondSubjectEnum.InoutTypeEnum.IN_TYPE.getType()).equals(billType)
                    || String.valueOf(BillSecondSubjectEnum.InoutTypeEnum.OUT_TYPE.getType()).equals(billType)) {
                //收入、支出
                billQueryRequest.setInoutType(BillSecondSubjectEnum.InoutTypeEnum.getEnumByCode(Byte.valueOf(billType)));
            } else if (String.valueOf(BillFirstSubjectEnum.USER_PLATFORM_GIVEN.getCode()).equals(billType)) {
                //平台奖励
                billQueryRequest.setSecondCodes(BillSecondSubjectEnum.listInvestBonusEnum());
            } else {
                //通过一级科目进行筛选
                billQueryRequest.setBillFirstSubjectEnum(BillFirstSubjectEnum.getEnumByCode(billType));
            }
        }
    }

    private void requestType(String requestType, BillQueryRequest billQueryRequest) {
        if (StringUtils.isNotEmpty(requestType)) {
            //账单入口类型
            EntranceTypeEnum entranceTypeEnum = EntranceTypeEnum.getTypeEnumByCode(requestType);
            billQueryRequest.setEntranceTypeEnum(entranceTypeEnum);

            //投资账单和借款账单要指定二级科目进行筛选
            if (EntranceTypeEnum.INVEST_BILL.getCode().equals(requestType) && Objects.isNull(billQueryRequest.getSecondCodes())) {
                billQueryRequest.setSecondCodes(BillFirstSubjectEnum.containInvestBounsCode());
            } else if (EntranceTypeEnum.BORROW_BILL.getCode().equals(requestType)) {
                billQueryRequest.setSecondCodes(BillFirstSubjectEnum.containBorrowBounsCode());
            }
        }
    }

    private boolean displayTotalMes(String requestType, String indexType) {

        if (StringUtils.isNotEmpty(indexType)) {
            return true;
        } else if (StringUtils.isNotEmpty(requestType)) {
            for (EntranceTypeEnum entranceEnum : EntranceTypeEnum.values()) {
                if (entranceEnum.getCode().equals(requestType)) {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 所有账单的入口类型
     * @return
     */
    private List<BillCommonTypeVO> allBill() {
        List<BillCommonTypeVO> list = new ArrayList<>();
        for (BillSecondSubjectEnum.InoutTypeEnum typeEnum : BillSecondSubjectEnum.InoutTypeEnum.values()) {
            list.add(new BillCommonTypeVO(String.valueOf(typeEnum.getType()), typeEnum.getDesc()));
        }

        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_RECHARGE.getIndexCode(), IndexBillTypeEnum.INDEX_RECHARGE.getIndexDesc()));
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_WITHDRAW.getIndexCode(), IndexBillTypeEnum.INDEX_WITHDRAW.getIndexDesc()));
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_INVEST.getIndexCode(), IndexBillTypeEnum.INDEX_INVEST.getIndexDesc()));
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_INVEST_BONUS.getIndexCode(), IndexBillTypeEnum.INDEX_INVEST_BONUS.getIndexDesc()));
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_REPAYMENT.getIndexCode(), IndexBillTypeEnum.INDEX_REPAYMENT.getIndexDesc()));
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_INVEST_BACK.getIndexCode(), IndexBillTypeEnum.INDEX_INVEST_BACK.getIndexDesc()));
        return list;
    }

    /**
     * 投资账单的入口类型
     * @return
     */
    private List<BillCommonTypeVO> investBill() {
        List<BillCommonTypeVO> list = new ArrayList<>();
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_INVEST.getIndexCode(), IndexBillTypeEnum.INDEX_INVEST.getIndexDesc()));
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_INVEST_BACK.getIndexCode(), IndexBillTypeEnum.INDEX_INVEST_BACK.getIndexDesc()));
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_INVEST_BONUS.getIndexCode(), IndexBillTypeEnum.INDEX_INVEST_BONUS.getIndexDesc()));

        return list;
    }

    /**
     * 担保账单的入口类型
     * @return
     */
    private List<BillCommonTypeVO> guaranteeBill() {
        List<BillCommonTypeVO> list = new ArrayList<>();
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_GUARANTEE_OUT.getIndexCode(), IndexBillTypeEnum.INDEX_GUARANTEE_OUT.getIndexDesc()));
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_GUARANTEE_REPAYMENT.getIndexCode(), IndexBillTypeEnum.INDEX_GUARANTEE_REPAYMENT.getIndexDesc()));
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_GUARANTEE_PAY.getIndexCode(), IndexBillTypeEnum.INDEX_GUARANTEE_PAY.getIndexDesc()));
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_GUARANTEE_BACK.getIndexCode(), IndexBillTypeEnum.INDEX_GUARANTEE_BACK.getIndexDesc()));
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_GUARANTEE_IN.getIndexCode(), IndexBillTypeEnum.INDEX_GUARANTEE_IN.getIndexDesc()));

        return list;
    }

    /**
     * 消息通知账单入口类型
     * @return
     */
    private List<BillCommonTypeVO> informBill() {
        List<BillCommonTypeVO> list = new ArrayList<>();
        list.add(new BillCommonTypeVO(IndexBillTypeEnum.INDEX_INVEST_BACK.getIndexCode(), IndexBillTypeEnum.INDEX_INVEST_BACK.getIndexDesc()));
        return list;
    }

}
